package cn.iocoder.yudao.module.cmt.service.person;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.person.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.person.PersonVolunteerApplyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cmt.dal.mysql.person.PersonVolunteerApplyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

/**
 * 志愿者/师资注册申请 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PersonVolunteerApplyServiceImpl implements PersonVolunteerApplyService {

    @Resource
    private PersonVolunteerApplyMapper volunteerApplyMapper;

    @Override
    public Long createVolunteerApply(PersonVolunteerApplySaveReqVO createReqVO) {
        // 插入
        PersonVolunteerApplyDO volunteerApply = BeanUtils.toBean(createReqVO, PersonVolunteerApplyDO.class);
        volunteerApplyMapper.insert(volunteerApply);
        // 返回
        return volunteerApply.getId();
    }

    @Override
    public void updateVolunteerApply(PersonVolunteerApplySaveReqVO updateReqVO) {
        // 校验存在
        validateVolunteerApplyExists(updateReqVO.getId());
        // 更新
        PersonVolunteerApplyDO updateObj = BeanUtils.toBean(updateReqVO, PersonVolunteerApplyDO.class);
        volunteerApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteVolunteerApply(Long id) {
        // 校验存在
        validateVolunteerApplyExists(id);
        // 删除
        volunteerApplyMapper.deleteById(id);
    }

    private void validateVolunteerApplyExists(Long id) {
        if (volunteerApplyMapper.selectById(id) == null) {
            throw exception(VOLUNTEER_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public PersonVolunteerApplyDO getVolunteerApply(Long id) {
        return volunteerApplyMapper.selectById(id);
    }

    @Override
    public PageResult<PersonVolunteerApplyDO> getVolunteerApplyPage(PersonVolunteerApplyPageReqVO pageReqVO) {
        return volunteerApplyMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVolunteerApplyListByIds(List<Long> ids) {
        // 校验存在
        ids.forEach(this::validateVolunteerApplyExists);
        // 删除
        volunteerApplyMapper.deleteBatchIds(ids);
    }

}
